<?php namespace Phpcmf\Controllers;

class Excel extends \Phpcmf\Common
{


    public function index(){

        \Phpcmf\Service::V()->display('upload.html');
    }


    // excel文件读取
    public function read() {

        if ($_FILES['fileInput']['error'] === UPLOAD_ERR_OK) {
            $file = $_FILES['fileInput']['tmp_name'];
            // 使用PHPExcel等库来读取Excel文件数据
        } else {
            echo 'Error uploading file.';
        }

        $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file);
        $sheet = $spreadsheet->getActiveSheet();
        $res = [];
        // 读取excel文件表格
        foreach ($sheet->getRowIterator(1) as $row) {
            $tmp = [];
            foreach ($row->getCellIterator() as $cell) {
                $tmp[] = $cell->getFormattedValue();
            }
            $res[$row->getRowIndex()] = $tmp;
        }


        foreach($res as $key=>$v){
            if($v[0] == '产品'){
                $data=[
                    'seo_title' => $v[2],
                    'seo_des' => $v[3],
                    'keywords' => $v[4],
                    'description' => $v[5],
                ];
              \Phpcmf\Service::M()->db->table('dr_1_news')->where('id', $v[1])->update($data);
                exit('插入成功');
            }
            if($v[0] == '栏目'){
                  $data = \Phpcmf\Service::M()->db->table('dr_1_share_category')->where('id', $v[1])->get()->getResultArray();
                  $seoData = json_decode($data[0]['setting'],true);
                  $seoData['seo']['list_title'] =  $v[2];
                  $seoData['seo']['list_keywords'] = $v[4];
                  $seoData['seo']['list_description'] =  $v[3];
                  $param = [
                     'setting' => json_encode($seoData,true)
                  ];
                  \Phpcmf\Service::M()->db->table('dr_1_share_category')->where('id', $v[1])->update($param);

                  exit('插入成功');

            }
            exit('插入失败');
        }

    }

}